class Solution {
public:
    void reverseOne(string& s,int l,int r){
        for(int i = l,j = r;i < j;i++,j--){
            swap(s[i],s[j]);
        }
    }
    void deleteB(string& s){
        int slow = 0,fast = 0;
        int k = 0;
        while(s[fast] == ' '){
            fast++;
        }
        while(fast < s.size()){
           if(s[fast] != ' '){
                s[slow++] = s[fast];
            }
           if(s[fast] == ' ' && s[slow - 1] != ' ' && s[fast + 1] != '\0'){
                s[slow++] = ' ';
            }
            fast++;
        }
        if(s[slow - 1] == ' '){
            s.resize(slow - 1);
        }
        else s.resize(slow);
    }
    string reverseWords(string s) {
        deleteB(s);
        
        for(int i = 0,j = 0;i <= s.size();i++){
            if(s[i] == '\0' || s[i] == ' '){
                reverseOne(s,j,i - 1);
                j = i + 1;
            }
        }
        reverseOne(s,0,s.size()-1);
        return s;
    }
};
